//package com.dongyimai.oauth.config;
//
//import com.dongyimai.oauth.util.UserJwt;
//import com.offcn.entity.Result;
//import com.offcn.user.feign.UserFeign;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.authority.AuthorityUtils;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.User;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.security.oauth2.provider.ClientDetails;
//import org.springframework.security.oauth2.provider.ClientDetailsService;
//import org.springframework.stereotype.Service;
//import org.springframework.util.StringUtils;
//
///*****
// * 自定义授权认证类
// */
//@Service
//public class UserDetailsServiceImpl implements UserDetailsService {
//
//    @Autowired
//    ClientDetailsService clientDetailsService;
//
//    @Autowired
//    private UserFeign userFeign;
//
//
//    /****
//     * 自定义授权认证
//     * @param username
//     * @return
//     * @throws UsernameNotFoundException
//     */
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        // =====================客户端信息认证开始============================
//        //取出身份，如果身份为空说明没有认证
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        //没有认证统一采用httpbasic认证，httpbasic中存储了client_id和client_secret，开始认证client_id和client_secret
//        if(authentication==null){
//            ClientDetails clientDetails = clientDetailsService.loadClientByClientId(username);
//            if(clientDetails!=null){
//                //秘钥
//                String clientSecret = clientDetails.getClientSecret();
//                //静态方式
//                //return new User(username,new BCryptPasswordEncoder().encode(clientSecret), AuthorityUtils.commaSeparatedStringToAuthorityList(""));
//                //数据库查找方式
//                return new User(username, // 客户ID
//                        clientSecret, // 客户端秘钥 --> 加密操作
//                        AuthorityUtils.commaSeparatedStringToAuthorityList("")); // 权限
//            }
//        }
//        // =====================客户端信息认证结束============================
//        // =====================用户账号和密码认证开始============================
//
//        if (StringUtils.isEmpty(username)) {
//            return null;
//        }
//
//        System.out.println("nihaooagsougosaug");
//        Result<com.offcn.user.pojo.User> result = userFeign.findUsername(username);
//        if (result.getData() == null || result.getData().getPassword() == null){
//            return null;
//        }
//        // 客户端ID  ： dongyimai
//        // 客户端秘钥: dongyimai
//        // 普通用户 --> 账号: 任意账号  密码：dongyimai
//        String password = result.getData().getPassword();
//        System.out.println(password);
//
//        //创建User对象
//        String permissions = "salesman,accountant,user,admin";
//        UserJwt userDetails = new UserJwt(username,password,AuthorityUtils.commaSeparatedStringToAuthorityList(permissions));
//        // =====================用户账号和密码认证结束============================
//        return userDetails;
//
//
//    }
//}
//
//
//
package com.dongyimai.oauth.config;

import com.dongyimai.oauth.util.UserJwt;
import com.offcn.entity.Result;
import com.offcn.user.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/*****
 * 自定义授权认证类
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {


    @Autowired
    ClientDetailsService clientDetailsService;

    @Autowired
    private UserFeign userFeign;

    /****
     * 自定义授权认证
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // =====================客户端信息认证开始============================
        //取出身份，如果身份为空说明没有认证
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //没有认证统一采用httpbasic认证，httpbasic中存储了client_id和client_secret，开始认证client_id和client_secret
        if (authentication == null) {
            ClientDetails clientDetails = clientDetailsService.loadClientByClientId(username);
            if (clientDetails != null) {
                //秘钥
                String clientSecret = clientDetails.getClientSecret();
                //静态方式
                //               return new User(username,new BCryptPasswordEncoder().encode(clientSecret), AuthorityUtils.commaSeparatedStringToAuthorityList(""));
                //数据库查找方式
                return new User(username, // 客户ID
                        clientSecret, // 客户端秘钥 --> 加密操作
                        AuthorityUtils.commaSeparatedStringToAuthorityList("")); // 权限
            }
        }
        // =====================客户端信息认证结束============================
        // =====================用户账号和密码认证开始============================

        if (StringUtils.isEmpty(username)) {
            return null;
        }

        // 客户端ID  ： dongyimai
        // 客户端秘钥: dongyimai
        // 普通用户 --> 账号: 任意账号  密码：dongyimai

        //根据用户名查询用户信息
        Result<com.offcn.user.pojo.User> result = userFeign.findUsername(username);
        if (result.getData() == null || result.getData().getPassword() == null) {
            return null;
        }

        String password = result.getData().getPassword();

        //创建User对象
        String permissions = "salesman,accountant,user,admin";
        UserJwt userDetails = new UserJwt(username, password, AuthorityUtils.commaSeparatedStringToAuthorityList(permissions));
        // =====================用户账号和密码认证结束============================
        return userDetails;


    }
}
